package com.ruoyi.web.controller.member.app;

import cn.hutool.core.util.ObjectUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.domain.MemberCardCreateVO;
import com.ruoyi.common.core.domain.entity.MemberCard;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.model.LoginAppBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.AppLoginService;
import com.ruoyi.member.service.IMemberUserService;
import com.ruoyi.common.core.domain.MemberUserUpdateReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "app登录")

@RestController
@RequestMapping("/member/auth")

public class AuthController extends BaseController {
    @Autowired
    private AppLoginService appLoginService;
    @Autowired
    private IMemberUserService appUserService;
    @Autowired
    private EasyEntityQuery easyEntityQuery;

    @ApiOperation("用户信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        return success(appUserService.selectAppUserByUserId(SecurityUtils.getAppUserId()));
    }
    @ApiOperation("用户信息修改-先获取用户信息")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MemberUserUpdateReqVo reqVo) {
        return success(appUserService.apiUpdateAppUser(reqVo));
    }
    @ApiOperation("添加卡信息")
    @PostMapping("/addCard")
    public AjaxResult addCard(@RequestBody MemberCardCreateVO reqVo) {
        MemberCard memberCard=new MemberCard();
        memberCard.setMemberId(SecurityUtils.getUserId());
        memberCard.setChName(reqVo.getChName());
        memberCard.setChIdNo(reqVo.getChIdNo());
        memberCard.setChCardNo(reqVo.getChCardNo());
        memberCard.setChMobile(reqVo.getChMobile());
        memberCard.setProtocolNo(reqVo.getProtocolNo());
        return toAjax(easyEntityQuery.insertable(memberCard).executeRows()>0);
    }
    @ApiOperation("卡信息")
    @PostMapping("/getCardList")
    public AjaxResult getCardList() {
       ;
        return success(easyEntityQuery.queryable(MemberCard.class).where(m -> m.memberId().eq(SecurityUtils.getUserId())).toList());
    }
    @ApiOperation("模拟登录")
    @PostMapping("/moni")
    public AjaxResult moni(String openId) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.moni(openId);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation("微信公众号登录")
    @GetMapping("/mpLogin")
    @Anonymous
    public AjaxResult mpLogin(String code){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.mpLogin(code);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @ApiOperation("微信公众号登录")
    @GetMapping("/mpOpenId")
    @Anonymous
    public AjaxResult mpOpenId(String code){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.mpLoginOpenId(code);
        ajax.put("openid", token);
        return ajax;
    }

    @ApiOperation("微信小程序登录")
    @GetMapping("/miniLogin")
    @Anonymous
    public AjaxResult miniLogin(String code){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.minLogin(code);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @ApiOperation("账号登录")
    @PostMapping("/userNameLogin")
    @Anonymous
    public AjaxResult userNameLogin(@RequestBody LoginAppBody loginAppBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.login(loginAppBody.getUsername(), loginAppBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @ApiOperation("注册账号")
    @PostMapping("/userNameReg")
    @Anonymous
    public AjaxResult userNameReg(@RequestBody LoginAppBody loginAppBody) {
        MemberUser appUser = appUserService.selectAppUserByUserName(loginAppBody.getUsername());
        if(ObjectUtil.isNotNull(appUser)){
            return AjaxResult.error("用户已被注册！");
        }
        MemberUser appUser1 = new MemberUser();
        appUser1.setUserName(loginAppBody.getUsername());
        appUser1.setPassword(loginAppBody.getPassword());

        appUserService.regAppUser(appUser1);
        return AjaxResult.success();
    }


}
